package com.ruicar.afs.cloud.channel.channelworkflow.processor;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.admin.api.feign.AfsRoleFeign;
import com.ruicar.afs.cloud.channel.channelworkflow.entity.WorkTaskDetail;
import com.ruicar.afs.cloud.channel.channelworkflow.enums.QueryRoleType;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskDetailService;
import com.ruicar.afs.cloud.channel.common.enums.ChannelAfsFlowEnum;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.security.vo.RoleVo;
import com.ruicar.afs.cloud.common.core.security.vo.UserVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.core.util.SpringContextHolder;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.CommonGetRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.GetAssignUserRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.UserOnlineRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.*;
import com.ruicar.afs.cloud.workflow.gwt.processor.AfsWorkFLowQueryProcessor;
import com.ruicar.afs.cloud.workflow.gwt.processor.DefaultAfsWorkFLowQueryProcessor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author limingzhi
 */
@Slf4j
@AllArgsConstructor
@Component
public class ChannelOnlineQueryProcessor implements AfsWorkFLowQueryProcessor {

    private final AfsRoleFeign afsRoleFeign;
    private final WorkTaskDetailService workTaskDetailService;
    @Override
    public List<String> afsFLowKeys() {
        return Arrays.asList(new String[]{
                //渠道上线：新车，二手车
                AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_ONLINE_NEW)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_ONLINE_OLD)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_REGISTER)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.COMMON_CAR_DEALER)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.DIRECT_CAR_DEALER)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.PAYMENT_ACCOUN)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_WITNESS)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_BLACK_JOIN)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_BLACK_RELIEVE)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.AFFILIATED_THIRD)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_QUOTA_NEW)
                , AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_QUOTA_OLD)
        });
    }

    @Override
    public List<UserOnlineResponse> checkUserOnline(UserOnlineRequest userOnlineRequest) {
        return null;
    }

    ///
    @Override
    public GetAssignUserResponse getAssignUser(GetAssignUserRequest getAssignUserRequest) {
        return null;
    }

    @Override
    public QueryOrgConditionResponse queryOrgCondition(CommonGetRequest commonGetRequest) {
        return null;
    }

    @Override
    public QueryRoleResponse queryRole(CommonGetRequest commonGetRequest) {
        log.info("设计器处理人类型获取 ==> {}", commonGetRequest);
        try {
            IResponse<List<RoleVo>> iResponse = afsRoleFeign.getAllList();
            Assert.isTrue(CommonConstants.SUCCESS.equalsIgnoreCase(iResponse.getCode()), "feign请求用户异常");
            QueryRoleResponse queryRoleResponse = new QueryRoleResponse();
            iResponse.getData().add(getRoleVO(QueryRoleType.APPOINT_USER));
            queryRoleResponse.setRoleVos(iResponse.getData());

            return queryRoleResponse;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }
    private RoleVo getRoleVO(QueryRoleType roleType) {
        RoleVo roleVo = new RoleVo();
        roleVo.setRoleCode(AfsEnumUtil.key(roleType));
        roleVo.setRoleName(AfsEnumUtil.desc(roleType));
        return roleVo;
    }

    @Override
    public Page<QueryUserResponse> queryUserInfo(QueryCondition<Map<String, Object>> queryCondition) {
        return SpringContextHolder.getBean(DefaultAfsWorkFLowQueryProcessor.class).queryUserInfo(queryCondition);
    }

    ///
    @Override
    public UserVo getRtnUserInfo(CommonGetRequest commonGetRequest) {
        //退回
        return SpringContextHolder.getBean(DefaultAfsWorkFLowQueryProcessor.class).getRtnUserInfo(commonGetRequest);
    }


    @Override
    public UserVo getApplyUserInfo(CommonGetRequest commonGetRequest) {
        return SpringContextHolder.getBean(DefaultAfsWorkFLowQueryProcessor.class).getApplyUserInfo(commonGetRequest);
    }

    @Override
    public UserVo getLeader(CommonGetRequest commonGetRequest) {
        //退回经销商
        return null;
    }

    @Override
    public UserVo getOperUser(CommonGetRequest commonGetRequest) {

        log.info("指定处理人获取 ==> {}", commonGetRequest);
        WorkTaskDetail detail = workTaskDetailService.getOne(Wrappers.<WorkTaskDetail>query().lambda()
                        .eq(WorkTaskDetail::getBizDataId, commonGetRequest.getBizDataId())
                        .orderByDesc(WorkTaskDetail::getCreateTime)
                , false
        );
        UserVo response = new UserVo();
        response.setUsername(detail.getCreateBy());
        log.info("指定处理人响应 ==> {}", response.getUsername());
        return response;
    }
}
